package com.mingqijia.gassafety.authority.vcc.oauth;

import com.gap.basic.base.CommonResponse;
import com.mingqijia.gassafety.shared.utils.ApplicationUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.InsufficientAuthenticationException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;

/**
 * 未认证入口点
 * @author Damon S.
 * @version v1.0.1
 * @date 2020年07月21日 08:19
 */
@Slf4j
@Component
public class UnauthorizedEntryPointImpl implements AuthenticationEntryPoint, Serializable {

    @Override
    public void commence(HttpServletRequest request,
                         HttpServletResponse response,
                         AuthenticationException failed) throws IOException, ServletException {
        String message = failed.getMessage();
        if (failed instanceof InsufficientAuthenticationException) {
            message = "权限不足";
        }
        ApplicationUtils.writeOutJson(response, CommonResponse.failure(message));
    }
}
